package com.tqz.rc.spring.cloud;

import com.tqz.rc.common.constant.DefaultValueConstant;
import com.tqz.rc.common.util.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.commons.util.InetUtils;
import org.springframework.cloud.commons.util.InetUtilsProperties;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import static com.tqz.rc.common.constant.FieldConstant.*;

/**
 * <p>注册中心的配置类。
 *
 * @author tianqingzhao
 * @since 2021/9/2 14:37
 */
@ConfigurationProperties(TqzRcProperties.PREFIX)
public class TqzRcProperties {

    /**
     * {@link TqzRcProperties} 的前缀。
     */
    public static final String PREFIX = "spring.cloud.tqz.rc";

    /**
     * 服务端地址。
     */
    private String serverAddr;

    /**
     * 实例的命名空间。
     */
    private String namespaceId = DefaultValueConstant.DEFAULT_NAMESPACE_ID;

    /**
     * 实例所在的服务名称。
     */
    @Value("${spring.cloud.tqz.rc.serviceName:${spring.application.name:}}")
    private String serviceName;

    /**
     * 实例的分组名称。
     */
    private String groupName = DefaultValueConstant.DEFAULT_GROUP;

    /**
     * 实例所在的集群名称。
     */
    private String clusterName = DefaultValueConstant.DEFAULT_CLUSTER_NAME;

    /**
     * 实例的ip
     */
    private String ip;

    /**
     * 实例的端口号。
     */
    private int port;

    /**
     * 是否是临时实例
     */
    private boolean ephemeral = true;

    /**
     * 是否开启自动注册功能。
     */
    private boolean registerEnable = true;

    private Map<String, String> metadata = new HashMap<>();

    @PostConstruct
    public void init() {
        if (StringUtils.isBlank(ip)) {
            ip = new InetUtils(new InetUtilsProperties()).findFirstNonLoopbackHostInfo().getIpAddress();
        }
    }

    public String getServerAddr() {
        return serverAddr;
    }

    public void setServerAddr(String serverAddr) {
        this.serverAddr = serverAddr;
    }

    public String getNamespaceId() {
        return namespaceId;
    }

    public void setNamespaceId(String namespaceId) {
        this.namespaceId = namespaceId;
    }

    public String getServiceName() {
        return serviceName;
    }

    public void setServiceName(String serviceName) {
        this.serviceName = serviceName;
    }

    public String getGroupName() {
        return groupName;
    }

    public void setGroupName(String groupName) {
        this.groupName = groupName;
    }

    public String getClusterName() {
        return clusterName;
    }

    public void setClusterName(String clusterName) {
        this.clusterName = clusterName;
    }

    public String getIp() {
        return ip;
    }

    public void setIp(String ip) {
        this.ip = ip;
    }

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }

    public boolean isEphemeral() {
        return ephemeral;
    }

    public void setEphemeral(boolean ephemeral) {
        this.ephemeral = ephemeral;
    }

    public boolean isRegisterEnable() {
        return registerEnable;
    }

    public void setRegisterEnable(boolean registerEnable) {
        this.registerEnable = registerEnable;
    }

    public Map<String, String> getMetadata() {
        return metadata;
    }

    public void setMetadata(Map<String, String> metadata) {
        this.metadata = metadata;
    }

    /**
     * 把配置文件转为 {@link Properties} 独享
     *
     * @return {@link Properties}
     */
    public Properties getTqzRcProperties() {
        Properties properties = new Properties();

        properties.setProperty(SERVER_ADDR_PARAM, serverAddr);
        properties.setProperty(NAMESPACE_ID_PARAM, namespaceId);
        properties.setProperty(SERVICE_NAME_PARAM, serviceName);
        properties.setProperty(GROUP_NAME_PARAM, groupName);
        properties.setProperty(CLUSTER_NAME_PARAM, clusterName);
        properties.setProperty(IP_PARAM, ip);
        properties.setProperty(PORT_PARAM, String.valueOf(port));
        properties.setProperty(REGISTER_ENBALE_PARAM, String.valueOf(registerEnable));

        // udp的端口号使用当前服务的端口号，nacos采用的是随机
        properties.setProperty("udp.port", String.valueOf(port));

        return properties;
    }
}
